package com.bestcem.xm.om.grpc.client;


import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 调用用户中心grpc
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2022/5/10 22:26
 */
@Slf4j
@Service
public class OmUserUserGrpcService {
    @GrpcClient(GrpcClientNames.DS_USER)
    private UserServiceGrpc.UserServiceBlockingStub stub;

    /**
     * 根据公司id获取管理员
     *
     * @param orgId
     * @return com.bestcem.bp.xm.user.grpc.v1.services.User
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/6/15 17:45
     */
    public User getAdminUserByOrgId(String orgId) {
        ListAdminsRequest.Builder builder = ListAdminsRequest.newBuilder();
        builder.setOrgId(orgId);
        try {
            ListAdminsResponse response = stub.listAdmins(builder.build());
            if (Code.OK_VALUE == response.getStatus().getCodeValue() && response.getUsersCount() > 0) {
                if (log.isInfoEnabled()) {
                    log.info("[OM]getAdminUserByOrgId方法执行");
                }
                return response.getUsers(0);
            }
        } catch (Exception e) {
            log.error("[OM] 调用user接口[查询机构信息]失败 orgId:{} ", orgId, e);
        }
        return null;
    }

    /**
     * 修改show_guide字段请求参数
     *
     * @param orgId
     * @return com.bestcem.bp.xm.user.grpc.v1.services.User
     * @author zhangyifang <yifang.zhang@idiaoyan.com>
     * @date 2022/6/15 17:45
     */
    public boolean updateShowGuideByOrgId(String orgId, Boolean showGuide) {
        UpdateShowGuideByOrgIdRequest.Builder builder = UpdateShowGuideByOrgIdRequest.newBuilder();
        builder.setOrgId(orgId);
        builder.setShowGuide(showGuide);
        try {
            UpdateShowGuideByOrgIdResponse response = stub.updateShowGuideByOrgId(builder.build());
            if (Code.OK_VALUE == response.getStatus().getCodeValue()) {
                if (log.isInfoEnabled()) {
                    log.info("[OM] (updateShowGuideByOrgId) user grpc返回：{}", response.getStatus().getCodeValue());
                }
                return true;
            }
        } catch (Exception e) {
            log.error("[OM] 调用user接口[查询机构信息]失败 orgId:{} ", orgId, e);
        }
        return false;
    }


    /**
     * 是否内部用户
     *
     * @param phone 手机号
     * @param email 邮箱
     * @return boolean
     * @author zhangfeng <feng.zhang@idiaoyan.com>
     * @date 2022/2/28 11:20
     */
    public boolean isInnerUser(String phone, String email) {
        try {
            CheckInnerRequest.Builder builder = CheckInnerRequest.newBuilder();
            if (Objects.nonNull(phone)) {
                builder.setPhone(phone);
            }
            if (Objects.nonNull(email)) {
                builder.setEmail(email);
            }
            CheckInnerRequest request = builder.build();
            CheckInnerResponse response = stub.checkInner(request);
            if (Objects.isNull(response) || Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
                log.error("[OM] 查询是否内部用户出错 phone: {}  email: {}", phone, email);
                return false;
            }
            if (log.isInfoEnabled()) {
                log.info("[OM] 查询是否内部用户 phone {} email {} inner {}", phone, email, response.getInner());
            }
            return response.getInner();
        } catch (Exception e) {
            log.error("[OM] 查询是否内部用户出错 e:", e);
            return false;
        }
    }

    public boolean isInnerUser(List<String> emails) {
        if (CollectionUtils.isEmpty(emails)) {
            return false;
        }
        for (String email : emails) {
            boolean flg = isInnerUser(null, email);
            if (!flg){
                return false;
            }
        }
        return true;
    }

    /**
     * 校验验证码
     *
     * @param prefix 前缀
     * @param phone  手机号
     * @param vcode  验证码
     * @return 校验结果
     */
    public ServiceResult<Boolean> checkVcode(String prefix, String phone, String vcode) {
        CheckVcodeRequest.Builder builder = CheckVcodeRequest.newBuilder();
        builder.setPrefix(prefix).setPhone(phone).setVcode(vcode);
        CheckVcodeResponse response = stub.checkVcode(builder.build());
        if (Code.OK_VALUE == response.getStatus().getCodeValue()) {
            if (log.isInfoEnabled()) {
                log.info("[OM] 检测验证码成功 prefix:{} phone:{} vcode:{}", prefix, phone, vcode);
            }
            return ServiceResult.success(response.getSuccess());
        } else {
            if (Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
                log.error("[OM] 获取行业信息出错 prefix:{} phone:{} vcode:{}", prefix, phone, vcode);
            }
            return ServiceResult.fail(response.getStatus().getMessage());
        }
    }
}
